package com.paishen.peiniwan.module.date;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.C;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.database.Gift;
import com.paishen.peiniwan.database.GiftDao;
import com.paishen.peiniwan.database.util.ModelConverter;
import com.paishen.peiniwan.frame.PnwOkCallbackAdapter;
import com.paishen.peiniwan.frame.http.PnwHttpManager;
import com.paishen.peiniwan.frame.http.response.HttpRespData;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.List;


public class DateGiftChooseLayout extends FrameLayout {

    public static final long TYPE_GIFT_UNSELECTED = 0;
    private long giftType = TYPE_GIFT_UNSELECTED;
    private long giftPrice = -1;

    private final View layGift1, layGift2, layGift3;
    private final TextView txGift1Value, txGift2Value, txGift3Value;
    private final TextView txGift1Unit, txGift2Unit, txGift3Unit;
    private final TextView txGift1, txGift2, txGift3;
    private final ImageView imgGift1, imgGift2, imgGift3;

    private final View layLoad;
    private final Button btnRetryLoad;
    private final View imgLoad;

    public DateGiftChooseLayout(Context context) {
        this(context, null);
    }

    public DateGiftChooseLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_date_gift_choose, this);

        imgGift1 = (ImageView) findViewById(R.id.img_gift_1);
        imgGift2 = (ImageView) findViewById(R.id.img_gift_2);
        imgGift3 = (ImageView) findViewById(R.id.img_gift_3);
        txGift1Value = (TextView) findViewById(R.id.tx_gift_1_value);
        txGift2Value = (TextView) findViewById(R.id.tx_gift_2_value);
        txGift3Value = (TextView) findViewById(R.id.tx_gift_3_value);
        txGift1Unit = (TextView) findViewById(R.id.tx_gift_1_unit);
        txGift2Unit = (TextView) findViewById(R.id.tx_gift_2_unit);
        txGift3Unit = (TextView) findViewById(R.id.tx_gift_3_unit);
        txGift1 = (TextView) findViewById(R.id.tx_gift_1);
        txGift2 = (TextView) findViewById(R.id.tx_gift_2);
        txGift3 = (TextView) findViewById(R.id.tx_gift_3);
        layGift1 = findViewById(R.id.lay_gift_1);
        layGift2 = findViewById(R.id.lay_gift_2);
        layGift3 = findViewById(R.id.lay_gift_3);
        layGift1.setOnClickListener(onGiftClickListener);
        layGift2.setOnClickListener(onGiftClickListener);
        layGift3.setOnClickListener(onGiftClickListener);

        layLoad = findViewById(R.id.lay_load);
        imgLoad= findViewById(R.id.img_load);
        btnRetryLoad =(Button) findViewById(R.id.btn_retry_load);
        btnRetryLoad.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                requestGiftGiftList();
            }
        });

        GiftDao giftDao = App.i().getCurrDaoSession().getGiftDao();
        List<Gift> giftList = giftDao.queryBuilder().list();
        if (giftList != null && giftList.size() > 0) {
            layLoad.setVisibility(View.GONE);
            setValue(giftList);
        } else {
            layLoad.setVisibility(View.VISIBLE);
        }

        requestGiftGiftList();
    }

    private void setValue(List<Gift> giftList) {

        layGift1.setVisibility(View.INVISIBLE);
        if (giftList.size() > 0) {
            layGift1.setVisibility(View.VISIBLE);
            Gift gift1 = giftList.get(0);
            Glide.with(getContext()).load(gift1.getImage()).placeholder(R.mipmap.common_pic_s_180).into(imgGift1);
            txGift1.setText(gift1.getTitle());
            txGift1Value.setText(String.valueOf((int) gift1.getPrice()));
            layGift1.setTag(gift1.getId());
        }
        layGift2.setVisibility(View.INVISIBLE);
        if (giftList.size() > 1) {
            layGift2.setVisibility(View.VISIBLE);
            Gift gift2 = giftList.get(1);
            Glide.with(getContext()).load(gift2.getImage()).placeholder(R.mipmap.common_pic_s_180).into(imgGift2);
            txGift2.setText(gift2.getTitle());
            txGift2Value.setText(String.valueOf((int) gift2.getPrice()));
            layGift2.setTag(gift2.getId());
        }
        layGift3.setVisibility(View.INVISIBLE);
        if (giftList.size() > 2) {
            layGift3.setVisibility(View.VISIBLE);
            Gift gift3 = giftList.get(2);
            Glide.with(getContext()).load(gift3.getImage()).placeholder(R.mipmap.common_pic_s_180).into(imgGift3);
            txGift3.setText(gift3.getTitle());
            txGift3Value.setText(String.valueOf((int) gift3.getPrice()));
            layGift3.setTag(gift3.getId());
        }
    }

    private View.OnClickListener onGiftClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            giftType = (long) v.getTag();
            switch (v.getId()) {
                case R.id.lay_gift_1:
                    layGift1.setBackgroundColor(0xFFFC4E52);
                    layGift2.setBackgroundColor(0x00000000);
                    layGift3.setBackgroundColor(0x00000000);
                    txGift1Value.setTextColor(0xFFFFFFFF);
                    txGift2Value.setTextColor(0xFFFC4E52);
                    txGift3Value.setTextColor(0xFFFC4E52);
                    txGift1Unit.setTextColor(0xFFFFFFFF);
                    txGift2Unit.setTextColor(0xFFFC4E52);
                    txGift3Unit.setTextColor(0xFFFC4E52);
                    txGift1.setTextColor(0xFFFFFFFF);
                    txGift2.setTextColor(0xFF05131A);
                    txGift3.setTextColor(0xFF05131A);
                    giftPrice = Integer.parseInt(txGift1Value.getText().toString());
                    break;
                case R.id.lay_gift_2:
                    layGift1.setBackgroundColor(0x00000000);
                    layGift2.setBackgroundColor(0xFFFC4E52);
                    layGift3.setBackgroundColor(0x00000000);
                    txGift1Value.setTextColor(0xFFFC4E52);
                    txGift2Value.setTextColor(0xFFFFFFFF);
                    txGift3Value.setTextColor(0xFFFC4E52);
                    txGift1Unit.setTextColor(0xFFFC4E52);
                    txGift2Unit.setTextColor(0xFFFFFFFF);
                    txGift3Unit.setTextColor(0xFFFC4E52);
                    txGift1.setTextColor(0xFF05131A);
                    txGift2.setTextColor(0xFFFFFFFF);
                    txGift3.setTextColor(0xFF05131A);
                    giftPrice = Integer.parseInt(txGift2Value.getText().toString());
                    break;
                case R.id.lay_gift_3:
                    layGift1.setBackgroundColor(0x00000000);
                    layGift2.setBackgroundColor(0x00000000);
                    layGift3.setBackgroundColor(0xFFFC4E52);
                    txGift1Value.setTextColor(0xFFFC4E52);
                    txGift2Value.setTextColor(0xFFFC4E52);
                    txGift3Value.setTextColor(0xFFFFFFFF);
                    txGift1Unit.setTextColor(0xFFFC4E52);
                    txGift2Unit.setTextColor(0xFFFC4E52);
                    txGift3Unit.setTextColor(0xFFFFFFFF);
                    txGift1.setTextColor(0xFF05131A);
                    txGift2.setTextColor(0xFF05131A);
                    txGift3.setTextColor(0xFFFFFFFF);
                    giftPrice = Integer.parseInt(txGift3Value.getText().toString());
                    break;
            }
        }
    };

    private void requestGiftGiftList() {
        imgLoad.setVisibility(View.VISIBLE);
        btnRetryLoad.setVisibility(View.GONE);
        PnwHttpManager.Gift.giftList(((BaseActivity) getContext()).TAG, new PnwOkCallbackAdapter<HttpRespData<List<com.paishen.peiniwan.frame.http.entity.Gift>>>() {
            @Override
            public void onResponse(HttpRespData<List<com.paishen.peiniwan.frame.http.entity.Gift>> response, boolean fresh) {
                super.onResponse(response, fresh);
                if (response.errcode == C.http.RESULT_SUCCESS) {
                    if (response.data != null && response.data.size() > 0) {
                        GiftDao giftDao = App.i().getCurrDaoSession().getGiftDao();
                        giftDao.deleteAll();
                        List<com.paishen.peiniwan.database.Gift> giftList = new ArrayList<>();
                        for (com.paishen.peiniwan.frame.http.entity.Gift gift : response.data) {
                            giftList.add(ModelConverter.mergeOrReplace(null, gift));
                        }
                        giftDao.insertInTx(giftList);

                        layLoad.setVisibility(View.GONE);
                        setValue(giftList);
                    } else {
                        imgLoad.setVisibility(View.GONE);
                        btnRetryLoad.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onFailure(Request request, Exception e) {
                imgLoad.setVisibility(View.GONE);
                btnRetryLoad.setVisibility(View.VISIBLE);
            }
        });
    }

    public long getGiftType() {
        return giftType;
    }

    public float getGiftPrice() {
        return giftPrice;
    }
}
